package com.lianzai.camera.controller;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.camera.model.CameraInfo;
import com.lianzai.camera.service.CameraService;
import com.lianzai.common.config.WxConfig;
import com.lianzai.common.controller.BaseController;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import com.lianzai.common.utils.PageUtils;
import com.lianzai.common.utils.Query;
import com.lianzai.common.utils.R;
import com.lianzai.mobile.dao.CommunityInfoDao;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.stat.service.StatService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/camera")
public class CameraInfoController extends BaseController {
	@Autowired
	private CameraService cameraService;

    @Autowired
    private CommunityInfoDao communityInfoDao;

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private StatService statService;

	@GetMapping()
	String black(){
		return "camera/list";
	}

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<CameraInfo> carInfoList = cameraService.list(query);
        int total = cameraService.count(query);
        PageUtils pageUtils = new PageUtils(carInfoList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "camera/add";
    }

    @GetMapping("/vedio/{id}")
    String vedio(@PathVariable("id") Long id, Model model){
        CameraInfo cameraInfo = cameraService.getCameraById(id);
        model.addAttribute("cameraInfo", cameraInfo);
	    return "camera/vedio";
    }

    @GetMapping("/plugin")
    String plugin(){
        return "camera/plugin";
    }

    @GetMapping("/chooseCommunity")
    String chooseCommunity(){
        return "camera/community";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:communityInfo:edit")
    String edit(@PathVariable("id") Long id, Model model){
        CameraInfo cameraInfo = cameraService.getCameraById(id);
        model.addAttribute("cameraInfo", cameraInfo);
        return "camera/edit";
    }

    @GetMapping("/qrcode/{id}")
    String qrcode(@PathVariable("id") Long id, Model model){
        CameraInfo cameraInfo = cameraService.getCameraById(id);
        model.addAttribute("cameraInfo", cameraInfo);
        model.addAttribute("payUrl", wxConfig.getWxMobileOrderUrl());
        return "camera/qrcode";
    }

    @ResponseBody
    @PostMapping("/save")
    public R save(CameraInfo cameraInfo)
    {
        int ret = cameraService.save(cameraInfo);
        // 对小区相机数量进行维护 20190728
        statService.addDeviceNum(cameraInfo.getCommunityId());

        if (ret > 0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ResponseBody
    @PostMapping("/update")
    public R update(CameraInfo cameraInfo)
    {
        int ret = cameraService.updateCamera(cameraInfo);

        if (ret > 0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @ResponseBody
    @PostMapping("/openGateByCarNumber")
    public R openGateByCarNumber(CameraInfo cameraInfo)
    {
        Integer communityId = cameraInfo.getCommunityId();
        Integer cameraId = cameraInfo.getId();
        String carNumber = cameraInfo.getCarNumber();
        Integer direction = cameraInfo.getDirection();
        //调本地系统 打开闸机
        CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(communityId+""));
        JSONObject cameraObject = new JSONObject();
        cameraObject.put("cameraId",cameraId);
        cameraObject.put("carNumber",carNumber);
        cameraObject.put("direction",direction);
        return LocalSvrSyncExecutor.openGateByCarNum(cameraObject,communityInfoDO);
    }

    @ResponseBody
    @PostMapping("/sync")
    public R sync(long cameraId)
    {
        int ret = cameraService.syncToLocalSystem(cameraId);

        if (ret > 0)
        {
            return R.ok("同步命令已执行");
        }
        else
        {
            return R.error();
        }
    }

    @ResponseBody
    @PostMapping("/delCamera")
    public R delCamera(CameraInfo cameraInfo)
    {
        int ret = cameraService.delCamera(cameraInfo);
        statService.subDeviceNum(cameraInfo.getCommunityId());
        if (ret > 0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @PostMapping( "/openGate")
    @ResponseBody
    public R openGate(HttpServletRequest request, @RequestBody JSONObject obj){
        Long communityId = obj.getLong("communityId");
        Long cameraId = obj.getLong("cameraId");
        //调本地系统 打开闸机
        CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(communityId+""));
        JSONObject cameraObject = new JSONObject();
        cameraObject.put("cameraId",cameraId);
        return LocalSvrSyncExecutor.openGate(cameraObject,communityInfoDO);
    }

}
